<template>
  <div class="seat-grid">
    <div class="grid-header">
      <h3>车厢 {{ carriage.carriageNumber }} 号 - {{ getCarriageTypeLabel(carriage.carriageType) }}</h3>
      <div class="legend">
        <span class="legend-item">
          <span class="seat available"></span>
          可用
        </span>
        <span class="legend-item">
          <span class="seat occupied"></span>
          已售
        </span>
        <span class="legend-item">
          <span class="seat window"></span>
          靠窗
        </span>
      </div>
    </div>

    <div class="seats-container">
      <div class="seats-grid">
        <div 
          v-for="seat in seats" 
          :key="seat.id"
          class="seat-item"
          :class="getSeatClasses(seat)"
          @click="handleSeatClick(seat)"
          :title="`座位: ${seat.seatNumber}, 类型: ${getSeatTypeLabel(seat.seatType)}, 价格: ¥${seat.price}`"
        >
          {{ seat.seatNumber }}
        </div>
      </div>
    </div>

    <div class="seat-info">
      <el-descriptions title="车厢信息" :column="2" border>
        <el-descriptions-item label="车厢号">{{ carriage.carriageNumber }}</el-descriptions-item>
        <el-descriptions-item label="车厢类型">{{ getCarriageTypeLabel(carriage.carriageType) }}</el-descriptions-item>
        <el-descriptions-item label="总座位数">{{ carriage.totalSeats }}</el-descriptions-item>
        <el-descriptions-item label="可用座位">{{ carriage.availableSeats }}</el-descriptions-item>
      </el-descriptions>
    </div>

    <!-- 座位编辑对话框 -->
    <el-dialog v-model="seatDialogVisible" title="座位详情" width="500px">
      <el-form :model="selectedSeat" v-if="selectedSeat" label-width="100px">
        <el-form-item label="座位号">
          <el-input v-model="selectedSeat.seatNumber" readonly />
        </el-form-item>
        <el-form-item label="座位类型">
          <el-tag>{{ getSeatTypeLabel(selectedSeat.seatType) }}</el-tag>
        </el-form-item>
        <el-form-item label="价格">
          <span>¥{{ selectedSeat.price }}</span>
        </el-form-item>
        <el-form-item label="状态">
          <el-tag :type="selectedSeat.isAvailable ? 'success' : 'danger'">
            {{ selectedSeat.isAvailable ? '可用' : '已售' }}
          </el-tag>
        </el-form-item>
      </el-form>
      
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="seatDialogVisible = false">关闭</el-button>
          <el-button 
            v-if="selectedSeat"
            :type="selectedSeat.isAvailable ? 'warning' : 'success'" 
            @click="toggleSeatStatus"
          >
            {{ selectedSeat.isAvailable ? '设为已售' : '设为可用' }}
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, defineProps, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import { seatApi } from '@/api/train'
import { CARRIAGE_TYPE_MAP, SEAT_TYPE_MAP } from '@/constants/train'
import type { Carriage, Seat, CarriageType, SeatType } from '@/types/train'

const props = defineProps<{
  carriage: Carriage
}>()

const emit = defineEmits<{
  refresh: []
}>()

const seats = ref<Seat[]>([])
const selectedSeat = ref<Seat | null>(null)
const seatDialogVisible = ref(false)

// 获取座位列表
const fetchSeats = async () => {
  try {
    const response = await seatApi.getSeatsByCarriageId(props.carriage.id)
    if (response.success) {
      seats.value = response.data
    } else {
      ElMessage.error(response.message)
    }
  } catch (error) {
    console.error('获取座位列表失败:', error)
    ElMessage.error('获取座位列表失败')
  }
}

// 处理座位点击
const handleSeatClick = (seat: Seat) => {
  selectedSeat.value = seat
  seatDialogVisible.value = true
}

// 切换座位状态
const toggleSeatStatus = async () => {
  if (!selectedSeat.value) return
  
  try {
    const response = await seatApi.toggleSeatAvailability(selectedSeat.value.id)
    if (response.success) {
      ElMessage.success('座位状态已更新')
      selectedSeat.value.isAvailable = !selectedSeat.value.isAvailable
      seatDialogVisible.value = false
      fetchSeats()
      emit('refresh')
    } else {
      ElMessage.error(response.message)
    }
  } catch (error) {
    console.error('更新座位状态失败:', error)
    ElMessage.error('更新座位状态失败')
  }
}

// 获取座位CSS类
const getSeatClasses = (seat: Seat) => {
  const classes = ['seat']
  
  if (seat.isAvailable) {
    classes.push('available')
  } else {
    classes.push('occupied')
  }
  
  if (seat.seatType === 'WINDOW') {
    classes.push('window')
  }
  
  return classes
}

// 获取车厢类型标签
const getCarriageTypeLabel = (type: CarriageType) => {
  return CARRIAGE_TYPE_MAP[type] || type
}

// 获取座位类型标签
const getSeatTypeLabel = (type: SeatType) => {
  return SEAT_TYPE_MAP[type] || type
}

onMounted(() => {
  fetchSeats()
})
</script>

<style scoped>
.seat-grid {
  padding: 20px;
}

.grid-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.legend {
  display: flex;
  gap: 15px;
  align-items: center;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
}

.seats-container {
  margin-bottom: 20px;
}

.seats-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  max-width: 400px;
  margin: 0 auto;
}

.seat-item {
  width: 60px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  font-size: 12px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s;
  border: 2px solid transparent;
}

.seat.available {
  background-color: #67c23a;
  color: white;
}

.seat.occupied {
  background-color: #f56c6c;
  color: white;
}

.seat.window {
  border-color: #409eff;
}

.seat-item:hover {
  transform: scale(1.1);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.seat-info {
  margin-top: 20px;
}
</style>